package com.gzhu.knowledgeAdmin.common.utils;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class JSCHTest {
    private static final Logger log = LoggerFactory.getLogger(JSCHTest.class);

//    public static List<String> remoteExecute(Session session, String command) throws JSchException {
////        log.debug(">> {}", command);
//        System.out.println(command);
//        List<String> resultLines = new ArrayList<>();
//        ChannelExec channel = null;
//        try{
//            channel = (ChannelExec) session.openChannel("exec");
//            channel.setCommand(command);
//            InputStream input = channel.getInputStream();
//            channel.connect();
//            try {
//                BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
//                String inputLine = null;
//                while((inputLine = inputReader.readLine()) != null) {
////                    log.debug("   {}", inputLine);
//                    System.out.println(inputLine);
//                    resultLines.add(inputLine);
//                }
//            } finally {
//                if (input != null) {
//                    try {
//                        input.close();
//                    } catch (Exception e) {
////                        log.error("JSch inputStream close error:", e);
//                        System.out.println("JSch inputStream close error:"+e);
//                    }
//                }
//            }
//        } catch (IOException e) {
////            log.error("IOcxecption:", e);
//            System.out.println(e.getMessage());
//        } finally {
//            if (channel != null) {
//                try {
//                    channel.disconnect();
//                } catch (Exception e) {
////                    log.error("JSch channel disconnect error:", e);
//                    System.out.println("JSch channel disconnect error:"+e);
//                }
//            }
//        }
//        return resultLines;
//    }
public static List<String> remoteExecute(Session session, String command) throws JSchException {
    log.debug(">> {}", command);
    List<String> resultLines = new ArrayList<>();
    ChannelExec channel = null;
    try{
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        InputStream input = channel.getInputStream();
        channel.connect();
        try {
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
            String inputLine = null;
            while((inputLine = inputReader.readLine()) != null) {
                log.debug("   {}", inputLine);
                resultLines.add(inputLine);
            }
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (Exception e) {
                    log.error("JSch inputStream close error:", e);
                }
            }
        }
    } catch (IOException e) {
        log.error("IOcxecption:", e);
    } finally {
        if (channel != null) {
            try {
                channel.disconnect();
            } catch (Exception e) {
                log.error("JSch channel disconnect error:", e);
            }
        }
    }
    return resultLines;
}



    public static void main(String[] args) {
        String user = "chaowei";
        String host = "172.23.206.76";
        int port = 22;
        String password = "TSWCbyy538";

        // 要执行的命令
//        String[] commands =
//                {"cd /home/chaowei/anaconda3/bin",
//                        ". ./activate",
//                        "conda activate PaddleSpeech",
//                        "ll"};

        JSch jsch = new JSch();
        Session session = null;

        try {
            // 创建并配置会话
            session = jsch.getSession(user, host, port);
            session.setPassword(password);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no"); // 注意：出于安全考虑，不建议在生产环境中使用
            session.setConfig(config);

            // 连接到会话
            session.connect();
            if (session.isConnected()) {
                System.out.println("Host connected."+session.getHost());
            }

            // 遍历命令并执行
//            for (String command : commands) {
//                executeCommand(session, command);
//            }
//            System.out.println(remoteExecute(session, "pwd"));
//            remoteExecute(session, "pwd");
//            remoteExecute(session, "ls");
//            remoteExecute(session, "conda activate PaddleSpeech;"
//            +"cd /home/chaowei/root/CPJ/PaddleSpeech/examples/other/tts_finetune/tts3;"+"ls;"
//                    +"python get_started.py;"+"/home/chaowei/root/CPJ/tts/input/csmsc_mini;" +
//                    "test7;"+"3;" +
//                    "/home/chaowei/root/CPJ/tts/output;");
//            remoteExecute(session,
//                    " python /home/chaowei/root/CPJ/PaddleSpeech/examples/other/tts_finetune/tts3/rpc_get_started.py /home/chaowei/root/CPJ/tts/input/csmsc_mini ll 5 /home/chaowei/root/CPJ/tts/output");
//            remoteExecute(session, ". ./activate");
//            remoteExecute(session, "conda activate PaddleSpeech");
//            remoteExecute(session, "ll");
            remoteExecute(session,"curl 'http://127.0.0.1:5001/mxxx?input=/home/chaowei/root/CPJ/tts/input/csmsc_mini&name=ljx&id=1&output=/home/chaowei/root/CPJ/tts/output'");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Thread.sleep(1200000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 确保会话在完成后被关闭
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }


}

